package com.yzh.openai.config;

import com.github.houbb.sensitive.word.api.IWordContext;
import com.github.houbb.sensitive.word.api.IWordReplace;
import com.github.houbb.sensitive.word.api.IWordResult;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.support.tag.WordTags;
import com.github.houbb.sensitive.word.utils.InnerWordCharUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 敏感词配置
 */
@Slf4j
@Configuration
public class SensitiveWordConfig {
    
    @Bean
    public SensitiveWordBs sensitiveWordBs() {
        String filePath = "senstive.txt";
        return SensitiveWordBs.newInstance()
                // 替换敏感词
                .wordReplace((stringBuilder, chars, iWordResult, iWordContext) -> {
                    String sensitiveWord = InnerWordCharUtils.getString(chars, iWordResult);
                    log.info("检测敏感词：{}", sensitiveWord);
                    int worldLength = iWordResult.endIndex() - iWordResult.startIndex();
                    for (int i = 0; i < worldLength; i++) {
                        stringBuilder.append("*");
                    }
                })
                // 是否忽略大小写
                .ignoreCase(true)
                // 是否忽略宽度
                .ignoreWidth(true)
                // 是否忽略数字样式
                .ignoreNumStyle(true)
                // 是否忽略中文样式
                .ignoreChineseStyle(true)
                // 是否忽略英文样式
                .ignoreEnglishStyle(true)
                // 是否忽略重复
                .ignoreRepeat(false)
                // 是否检查数字
                .enableNumCheck(true)
                // 是否检查邮箱
                .enableEmailCheck(true)
                // 是否检查url
                .enableUrlCheck(true)
                // 是否检查敏感词
                .enableWordCheck(true)
                // 数字检查长度
                .numCheckLen(1024)
                .init();
    }
}
